package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Security;
import com.wn55.hrms.entity.Trainer;
import com.wn55.hrms.service.SecurityService;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zy
 * @date 2022/5/7 10:32
 */
@RestController
@RequestMapping("security")
@Api(value = "此接口用于操作五险一金功能")
public class SecurityController {
    @Autowired
    SecurityService securityService;

    @GetMapping("/securityList")
    @ApiOperation(value = "分页查询员工社保")

    public ResultResponse<PageInfo<Security>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now ,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            String name){
        ResultResponse<PageInfo<Security>> result = null;

        try {
            PageInfo<Security> pageInfo = securityService.securityList(now,size,name);
            result = new ResultResponse<>(200,"查询社保成功!",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"查询社保失败!");
        }
        return result ;
    }


    @PutMapping("/modify")
    @ApiOperation(value = "修改员工社保")

    public ResultResponse<Void> modify(@RequestBody Security security){
        ResultResponse<Void> result = null;

        try {
            System.out.println(security.toString());
            securityService.modify(security);
            result = new ResultResponse<>(200,"修改社保成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"修改社保失败!");
        }
        return result ;
    }



    @PostMapping("/add")
    @ApiOperation(value = "添加员工社保")

    public ResultResponse<Void> add(@RequestBody Security trainer){
        ResultResponse<Void> result = null;


        try {

            securityService.add(trainer);
            result = new ResultResponse<>(200,"添加社保成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"添加社保失败!");
        }
        return result ;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除员工社保")

    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {
            securityService.removeBykey(id);
            result = new ResultResponse<>(200,"删除社保成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"删除社保失败!");
        }
        return result ;
    }



}
